package com.tianqu.tms.www.car.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarImage;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang 高级搜索后车辆的详细页controller
 */
public class CarSearchDetailController extends TmsController {

	private Logger logger = Logger.getLogger(CarSearchDetailController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Car car = Car.getInstance();
			CarCreationInputViewHelper viewHelper = new CarCreationInputViewHelper();
			String serial = (String) uc.getAttribute("serial");
			// 进入修改页面则从数据库中读出图片信息
			CarImage imgEntity = CarImage.getInstance();
			car = car.findCarBySerial(serial);
			setViewhelperFromEntity(viewHelper, car);
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");

	}

	/**
	 * 从Entity 给viewHelper 赋值
	 * 
	 * @param viewHelper
	 * @param car
	 * @throws TmsDaoException
	 */
	private void setViewhelperFromEntity(CarCreationInputViewHelper viewHelper,
			Car car) throws TmsDaoException {
		Codes code = Codes.getInstance();
		CarStyle carStyle = CarStyle.getInstance();
		viewHelper.setAttribute(Integer.toString(car.getAttribute()));
		viewHelper.setBrandId(car.getBrandId());
		viewHelper.setCardType(Integer.toString(car.getCardType()));
		// //去掉车牌号中的'
		// String licensePlate = car.getLicensePlate().replace("'", "");
		viewHelper.setId(car.getcId());
		viewHelper.setLicensePlate(car.getLicensePlate());
		viewHelper.setCarOwner(car.getCarOwner());
		viewHelper.setCarOwnerIdCardNo(car.getCarOwnerIdCardNo());
		viewHelper.setCarType(Integer.toString(car.getCarType()));
		viewHelper.setCarUse(Integer.toString(car.getCarUse()));
		viewHelper.setColor(Integer.toString( car.getColor()));
		viewHelper.setCompanyContactPerson(car.getCompanyContactPerson());
		viewHelper.setCompanyContactPhone(car.getCompanyContactPhone());
		viewHelper.setCountryStyle(Integer.toString( car.getCountryStyle()));
		if (car.getDisplacement() == 0) {
			viewHelper.setDisplacement("");
		} else {
			viewHelper.setDisplacement(Integer.toString(car.getDisplacement() * 1000));
		}

		viewHelper.setEngine(car.getEngine());
		viewHelper.setFuelType(Integer.toString(car.getFuelType()));
		viewHelper.setGearbox(Integer.toString(car.getGearbox()));
		viewHelper.setJqx(car.getJqx());
		if (car.getLoadquality() == 0) {
			viewHelper.setLoadquality("");
		} else {
			viewHelper.setLoadquality(Integer.toString(car.getLoadquality()));
		}

		viewHelper.setCarStyleId(car.getCarStyleId());
		viewHelper.setCarName(carStyle.getCarStyleRealNameById(car
				.getCarStyleId()));
		
		viewHelper.setMemberId(Integer.toString(car.getMemberId()));
		if (car.getMileage() == 0) {
			viewHelper.setMileage("");
		} else {
			viewHelper.setMileage(StringUtil.addDH(Integer.toString(car.getMileage())));
		}

		viewHelper.setModelCode(car.getModelCode());
		viewHelper.setOwnerAddress(car.getOwnerAddress());
		viewHelper.setOwnerCardNo(car.getOwnerCardNo());
		viewHelper.setOwnerOrgNo(car.getOwnerOrgNo());
		viewHelper.setOwnerPhone(car.getOwnerPhone());
		viewHelper.setOwnerType(Integer.toString(car.getOwnerType()));
		if (car.getPassenger() == 0) {
			viewHelper.setPassenger("");
		} else {
			viewHelper.setPassenger(Integer.toString(car.getPassenger()));
		}
		if (car.getPrice() == 0) {
			viewHelper.setPrice("");
		} else {
			viewHelper.setPrice(StringUtil.addDH(Integer.toString(car.getPrice())));
		}

		viewHelper.setRegDate(car.getRegDate());
		viewHelper.setRegisterCardNo(car.getRegisterCardNo());
		viewHelper.setSendCardDate(car.getSendCardDate());
		viewHelper.setSerial(car.getSerial());
		viewHelper.setUpdateTime(car.getUpdateTime());
		viewHelper.setUpdateUserId(car.getUpdateUserId());
		viewHelper.setVendorCode(car.getVendorCode());
		viewHelper.setVin(car.getVin());
		viewHelper.setYearCarefulPeriod(car.getYearCarefulPeriod());
		viewHelper.setRegisterStatus(car.getRegisterStatus());
		viewHelper.setCarScore(car.getCarScore());
		viewHelper.setEntryStatus(car.getEntryStatus());
		viewHelper.setInsertUserId(car.getInsertUserId());
		viewHelper.setInsertTime(car.getInsertTime());
		viewHelper.setVendorId(car.getVendorId());
		viewHelper.setCarStyle(car.getCarStyleId());
		
		viewHelper.setCarTypeList(code.getCodeslistByItemId(Codes.VEHICLE_TYPE));//车辆类型
		viewHelper.setColorList(code.getCodeslistByItemId(Codes.VEHICLE_COLOR));//车辆颜色
		viewHelper.setCarUseList(code.getCodeslistByItemId(Codes.USER_NATURE));// 使用性质list
		viewHelper.setCardTypeList(code.getCodeslistByItemId(Codes.PLATE_TYPE));// 号牌种类list
		viewHelper.setFuelTypeList(code.getCodeslistByItemId(Codes.FUEL_TYPE));// 燃料种类list
		viewHelper.setCountryStyleList(code.getCodeslistByItemId(Codes.VEHICLE_COUNTRY));// 国别list
		viewHelper.setAttributeList(code.getCodeslistByItemId(Codes.ATTACHMENT_PROPERTY));// 车辆状态list' 
		viewHelper.setOwnerTypeList(code.getCodeslistByItemId(Codes.CAR_OWNER));// 车主类型list
		viewHelper.setGearBoxList(code.getCodeslistByItemId(Codes.GEARBOX_TYPE));//变速箱类型list
		Member menber = Member.getInstance();
		viewHelper.setMemberList(menber.findAllMemInfo());// 会员类型
		

		//车辆品牌
		Vendor vendor = Vendor.getInstance();		
		viewHelper.setVendorList(vendor.findAllVendorInfo());// 车主类型list	
		viewHelper.setImg1(car.getPhoto_1());
		viewHelper.setImg2(car.getPhoto_2());
		viewHelper.setImg3(car.getPhoto_3());
		viewHelper.setImg4(car.getPhoto_4());
		viewHelper.setImg5(car.getPhoto_5());
		viewHelper.setImg6(car.getPhoto_6());
		viewHelper.setImg7(car.getPhoto_7());
		viewHelper.setImg8(car.getPhoto_8());
	}

}
